<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
    <% String path=request.getContextPath(); String basePath=request.getScheme() + "://" + request.getServerName() + ":"
        + request.getServerPort() + path + "/" ; %>

        <!DOCTYPE html>
        <html>

        <head>
            <base href="<%=basePath%>">

            <title>本部门督办项</title>

            <meta http-equiv="pragma" content="no-cache">
            <meta http-equiv="cache-control" content="no-cache">
            <meta http-equiv="expires" content="0">
            <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
            <meta http-equiv="description" content="This is my page">
            <!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

        </head>

        <body>
            <%@ include file="/WEB-INF/jsp/common/header.jsp" %>
                <%@ include file="/WEB-INF/jsp/common/loadDiv.jsp" %>
                    <script type="text/javascript">
                        $(function () {
                            $('#startTime').datebox('setValue', getBeforeCurrentDate30Days());
                            $('#endTime').datebox('setValue', getNowFormatDate());
                            bindgrid();

                            $("#uploadBtn").bind("click", function (e) {
                                $("#uploadpicker").click();
                            });
                        });
                    </script>

                    <style scoped="scoped">
                        .textbox {
                            height: 20px;
                            margin: 0;
                            padding: 0 2px;
                            box-sizing: content-box;
                        }

                        /*.table tr {
height: 15px;
}*/
                    </style>
                    <div class="query_criteria" id="query_criteria">
                        <div style="margin: 5px 0px">
                            <%-- 搜索框 --%>
                                <label style="font-size: 12px;">创建时间：</label> <input class="easyui-datebox"
                                    name="startTime" id="startTime"
                                    data-options="required:true,showSeconds:false,formatter:myformatter,parser:myparser"
                                    value="" style="width: 150px"> <label style="font-size: 10px;">~</label> <input
                                    class="easyui-datebox" name="endTime" id="endTime"
                                    data-options="required:true,showSeconds:false,formatter:myformatter,parser:myparser"
                                    style="width: 150px" /> <label
                                    style="margin-left: 20px; font-size: 12px;">状态：</label>
                                <input class="easyui-combobox" id="querystatus" name="querystatus" style="width: 80px;"
                                    data-options="valueField: 'value',editable:false,
						textField: 'label',panelHeight:'auto',data: [{
							label: '全部',
							value: ''
						},{
							label: '待提交',
							value: '0',
							selected:true
						},{
							label: '已提交',
							value: '1'
						}]">
                                </select> <a id="search" style="margin-left: 30px" href="javascript:void(0)"
                                    class="easyui-linkbutton" data-options="iconCls:'icon-search'"
                                    onclick="selectList()">查询</a> <a id="export" href="javascript:void(0)"
                                    class="easyui-linkbutton" data-options="iconCls:'icon-export'"
                                    onclick="exportList()">导出</a>
                        </div>

                        <table id="dg"></table>

                        <%-- 新增/编辑/详情 --%>
                            <div id="add" class="easyui-window" closed="true" modal="true" title="详细信息"
                                style="width: 930px; height: 400px; padding: 5px 5px; top: 50px;">
                                <div id="tabs" class="easyui-tabs" style="width: 885px; height: 350px">
                                    <div title="基本信息" style="padding: 5px;">
                                        <form id="fm" class="ssec-form">
                                            <table class="table">
                                                <tr>
                                                    <td>
                                                        <%-- 预防纠正id--%>
                                                            <input id="corId" type="hidden" name="corId" />
                                                            <div class="ssec-label">预防编码：</div>
                                                    </td>
                                                    <td>
                                                        <div class="ssec-text normal">
                                                            <input id="corNum" name="corNum" class="easyui-textbox"
                                                                data-options="required:false,readonly:true" />
                                                        </div>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        <div class="ssec-label">预防纠正标题：</div>
                                                    </td>
                                                    <td>
                                                        <div class="ssec-text normal">
                                                            <input id="corTitle" name="corTitle" class="easyui-textbox"
                                                                data-options="required:false,readonly:true" />
                                                        </div>
                                                    </td>
                                                </tr>
                                            </table>
                                        </form>
                                    </div>
                                    <div title="督办项" style="padding: 5px;">
                                        <table id="supervise"></table>
                                    </div>
                                </div>
                            </div>
                            <%-- 行动项详情 --%>
                                <div id="supviseDetaillWindow" class="easyui-window" closed="true" modal="true"
                                    title="督办项详情" style="width: 930px; height: 400px; padding: 5px 5px; top: 50px;">
                                    <div id="supviseTabs" class="easyui-tabs" data-options="selected:0"
                                        style="width: 885px; height: 350px">

                                        <div title="督办项详情" style="padding: 5px;">
                                            <form id="superviesForm">
                                                <table>
                                                    <input id="supId" name="supId" type="hidden" />
                                                    <input id="dutyPersonCode" name="dutyPersonCode" type="hidden" />
                                                    <input id="dutyDeptCode" name="dutyDeptCode" type="hidden" />
                                                    <tr>
                                                        <td>
                                                            <div class="ssec-label">计划完成时间：</div>
                                                        </td>
                                                        <td>
                                                            <div class="ssec-text normal">
                                                                <input id="planCompleteDate" name="planCompleteTime"
                                                                    class="easyui-datebox"
                                                                    data-options="required:true,showSeconds:false,formatter:myformatter,parser:myparser" />
                                                            </div>
                                                        </td>
                                                        <td>
                                                            <div class="ssec-label">实际完成时间：</div>
                                                        </td>
                                                        <td>
                                                            <div class="ssec-text normal">
                                                                <input id="realCompleteDate" name="realCompleteDate"
                                                                    class="easyui-textbox"
                                                                    data-options="readonly:true" />
                                                            </div>
                                                        </td>
                                                        <td>
                                                            <div class="ssec-label">责任部门名称：</div>
                                                        </td>
                                                        <td>
                                                            <div class="ssec-text normal">
                                                                <input id="dutyDeptName" name="dutyDeptName"
                                                                    class="easyui-textbox"
                                                                    data-options="readonly:true" />
                                                            </div>
                                                        </td>
                                                    </tr>
                                                    <tr>

                                                        <td>
                                                            <div class="ssec-label">责任人：</div>
                                                        </td>
                                                        <td>
                                                            <div class="ssec-text normal">
                                                                <input id="dutyPersonName" name="dutyPersonName"
                                                                    class="easyui-textbox"
                                                                    data-options="readonly:true" />
                                                            </div>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            <div class="ssec-label">存在问题：</div>
                                                        </td>
                                                        <td colspan="4">
                                                            <div class="ssec-text normal">
                                                                <input id="problem" name="problem"
                                                                    class="easyui-textbox"
                                                                    style="width:250px;height:60px"
                                                                    data-options="readonly:true,multiline:true" />
                                                            </div>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            <div class="ssec-label">原因分析：</div>
                                                        </td>
                                                        <td colspan="4">
                                                            <div class="ssec-text normal">
                                                                <input id="cause" name="cause" class="easyui-textbox"
                                                                    style="width:250px;height:60px"
                                                                    data-options="required:true,readonly:false,multiline:true" />
                                                            </div>
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            <div class="ssec-label">预防纠正措施：</div>
                                                        </td>
                                                        <td colspan="4">
                                                            <div class="ssec-text normal">
                                                                <input id="action" name="action" class="easyui-textbox"
                                                                    style="width:250px;height:60px"
                                                                    data-options="required:true,readonly:false,multiline:true" />
                                                            </div>
                                                        </td>
                                                    </tr>


                                                    <tr>
                                                        <td>
                                                            <div class="ssec-label">整改描述：</div>
                                                        </td>
                                                        <td colspan="5">
                                                            <div class="ssec-text normal">
                                                                <input id="reformStatus" name="reformStatus"
                                                                    class="easyui-textbox"
                                                                    style="width:350px;height:60px"
                                                                    data-options="required:true,readonly:false,multiline:true" />
                                                            </div>
                                                        </td>
                                                    </tr>
                                                </table>
                                            </form>
                                        </div>

                                        <div title="附件" style="padding: 5px;">
                                            <table>
                                                <tr>
                                                    <td colspan="3"><input type="button" id="uploadBtn"
                                                            value="选择文件..." />
                                                        <input type="file" id="uploadpicker" accept=""
                                                            onchange="changeFile()" multiple style="display: none;" />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td><label style="width: 40%; text-align: right;">附件：</label>
                                                        <ul id="checkfileList"></ul>
                                                    </td>
                                                </tr>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                    </div>
        </body>
        <script type="text/javascript">
            var selectedFileList = [];//已选文件列表
            var succsessCount = 0;//上传成功文件个数
            var errorCount = 0;//上传失败文件个数
            var editIndex = undefined;

            // 行编辑结束
            function endEditing() {

                if (editIndex == undefined) {
                    return true
                }
                if ($('#supervise').datagrid('validateRow', editIndex)) {
                    $('#supervise').datagrid('endEdit', editIndex);
                    editIndex = undefined;
                    return true;
                } else {
                    $.messager.alert('提示', '有未填写数据!')
                    return false;
                }
            }


        </script>
        <script>
            function del(node) {
                $.ajax({
                    type: "delete",
                    //url: 'oos/corrective/supervise/delete?'  +'supId=' + node.supId ,
                    //                dataType: "xml",
                    success: function (data) {
                        $.messager.alert('消息', data.msg);
                        $('#add').window('close');
                        selectList();
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        $.messager.alert('消息', errorThrown);
                    }
                });
            }

            function back(node) {
                $.ajax({
                    type: "get",
                    url: 'oos/corrective/supervise/back?' + 'supId=' + node.supId,
                    //                dataType: "xml",
                    success: function (data) {
                        $.messager.alert('消息', data.msg);
                        $('#add').window('close');
                        selectList();
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        $.messager.alert('消息', errorThrown);
                    }
                });
            }

            function save(action) {
                const supervises = $('#supervise').datagrid('getRows')
                let model = {
                    corId: $('#corId').val(),
                    status: '0',
                    supervises: supervises
                };
                saveAjax(model, action)
            }

            function saveAjax(model, action) {
                let corId = model.corId
                $.ajax({
                    type: "post",
                    dataType: "json",
                    url: 'oos/corrective/action/updateForDutyDept',
                    data: JSON.stringify(model),
                    contentType: "application/json",
                    success: function (data) {
                    },
                    complete: function (XMLHttpRequest) {
                        if (XMLHttpRequest.responseJSON.code != 60200) {
                            $.messager.alert('消息', XMLHttpRequest.responseJSON.msg);
                        } else {
                            if ('save' == action) {
                                $.messager.show({
                                    title: '提示',
                                    msg: XMLHttpRequest.responseJSON.msg,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                                let dg = $('#dg');
                                dg.datagrid('enableFilter');
                                dg.datagrid('load');
                                $('#add').window('close');
                                detailAjax(corId)
                            } else {
                                commit(model)
                            }
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        $.messager.alert('消息', errorThrown);
                    }
                })

            }

            function commit(obj) {
                obj.status = '1'
                // 必须上传附件
                if (obj.supervises && obj.supervises.length > 0) {
                    let attchTag = obj.supervises.every(item => {
                        const attchArrTmp = queryAttch(item.supId, "oos_corrective_supervise")
                        return (attchArrTmp && attchArrTmp.length > 0)
                    })
                    if (!attchTag) {
                        $.messager.alert('消息', '每条行动项必须上传附件！');
                        return
                    }
                }
                $.ajax({
                    type: "post",
                    dataType: "json",
                    url: 'oos/corrective/action/updateForDutyDept',
                    data: JSON.stringify(obj),
                    contentType: "application/json",
                    success: function (data) {
                        if (data.code != 60200) {
                            $.messager.alert('消息', data.msg);
                            selectList();
                        } else {
                            $.messager.alert('消息', data.msg);
                            let dg = $('#dg');
                            dg.datagrid('enableFilter');
                            dg.datagrid('load');
                            $('#add').window('close');
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        $.messager.alert('消息', errorThrown);
                    }
                })
            }

            /**
             * 根据主键id与表明查询是否有附件
             */
            function queryAttch(key, fileTable) {
                let dataArray = null
                $.ajax({
                    type: "post",
                    url: "file/getFileList",
                    async: false,
                    data: {
                        fileTable: fileTable,
                        fileRow: key
                    },
                    success: function (data) {
                        dataArray = eval(data);
                        // initTaskFile(dataArray, row.status);
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        //  $.messager.alert('消息', errorThrown);
                    }
                });
                return dataArray;
            }

            /**
             * 保存附件
             */
            function saveAttch(row, obj, action) {
                obj.supId = row
                let saveNum = 0
                if (selectedFileList.length > 0) {
                    $.each(selectedFileList, function (index, item) {
                        showFilediv();
                        var dataform = new FormData();
                        dataform.append("file", item);
                        var xmlhttp = null;
                        if (window.XMLHttpRequest) {// code for all new browsers
                            xmlhttp = new XMLHttpRequest();
                        } else if (window.ActiveXObject) {// code for IE5 and IE6
                            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                        }
                        if (xmlhttp != null) {
                            xmlhttp.open("POST", "api/file/springUpload?fileRow=" + row
                                + "&fileTable=oos_corrective_supervise&fileName="
                                + item.name, true);
                            xmlhttp.setRequestHeader('token', localStorage.getItem('token'));
                            xmlhttp.setRequestHeader('userType', localStorage.getItem('userType'));
                            xmlhttp.send(dataform);
                            xmlhttp.onreadystatechange = callbackMethods;
                        } else {
                            alert("你的浏览器版本不兼容，请使用更高版本的浏览器");
                        }

                        function callbackMethods() {
                            if (xmlhttp.readyState == 4) {
                                if (xmlhttp.status == 200) {
                                    saveNum += 1
                                }
                                if (saveNum == selectedFileList.length) {
                                    hideFilediv();
                                    $('#supviseDetaillWindow').window('close')
                                }
                            } else {
                                if (xmlhttp.status == 500) {

                                }
                            }
                        }
                    });
                } else {
                    $('#supviseDetaillWindow').window('close')
                }
            }

            function editrow(row) {
                //编辑用户
                var row = $('#dg').datagrid('getSelected');
                if (row) {
                    $('#add').window('open');
                    $('#fm').form('load', row);
                    initBtn(row.status);

                } else {
                    $.messager.alert('消息', '请选中一条数据');
                }
            }
        </script>
        <script>

            function initBtn(status) {
                if (status == '0') {
                    $('#tabs').tabs({
                        tools: [{
                            text: '保存',
                            iconCls: 'icon-save',
                            handler: function () {
                                save('save');
                            }
                        }, {
                            text: '提交',
                            iconCls: 'icon-ok',
                            handler: function () {
                                $.messager.confirm('提示', '提交后无法再次编辑，确定要提交吗?',
                                    function (r) {
                                        if (r) {
                                            save('submit');
                                        }
                                    });
                            }
                        }]
                    })
                } else {
                    $('#tabs').tabs({
                        tools: null
                    })
                }
            }

            /**
             *  按条件查询报价列表
             */
            function selectList() {
                var startTime = $('#startTime').datebox('getValue')
                var endTime = $('#endTime').datebox('getValue')
                var status = $('#querystatus').combobox('getValue');
                var dg = $('#dg');
                dg.datagrid('enableFilter');
                dg.datagrid('load', {
                    startTime: startTime ? startTime : '',
                    endTime: endTime ? endTime : '',
                    status: status ? status : ''
                });
            }

            function bindgrid() {
                var startTime = $('#startTime').datebox('getValue')
                var endTime = $('#endTime').datebox('getValue')
                var status = $('#querystatus').combobox('getValue');
                $('#dg').datagrid({
                    title: '',
                    queryParams: {
                        startTime: startTime,
                        endTime: endTime,
                        status: status
                    },
                    url: "oos/corrective/supervise/selectDepartmentalList",
                    iconCls: 'icon-application',
                    width: getWidth(0.98),
                    height: 400,
                    nowrap: true,
                    filterDelay: 0,
                    rownumbers: true,
                    animate: false,
                    collapsible: false,
                    clientPaging: false,
                    remoteFilter: true,
                    singleSelect: true,
                    pagination: true,
                    remoteSort: true,
                    frozenColumns: [[]],
                    columns: [
                        [
                            {
                                field: 'corId',
                                title: '预防纠正id',
                                sortable: true,
                                hidden: true,
                                formatter: function (val, rec) {
                                    return val ? unescape(val) : '';
                                }
                            }, {
                                field: 'corNum',
                                title: '编码',
                                sortable: true,
                                formatter: function (val, rec, index) {
                                    return '<a style="text-decoration: none;" href="javascript:void(0)" onclick="detail(' + index +
                                        ')">' + (val ? val : '') + '</a>'
                                }
                            }, {
                                field: 'corTitle',
                                title: '预防纠正标题',
                                sortable: true,
                                formatter: function (val, rec) {
                                    return val ? unescape(val) : '';
                                }
                            }, {
                                field: 'status',
                                title: '状态',
                                sortable: true,
                                formatter: function (val, rec) {
                                    return val == '1' ? '已完成' : '待完成';
                                }
                            }, {
                                field: 'createId',
                                title: '创建人id',
                                sortable: true,
                                hidden: true,
                                formatter: function (val, rec) {
                                    return val ? unescape(val) : '';
                                }
                            }, {
                                field: 'createTime',
                                title: '创建时间',
                                sortable: true,
                                formatter: function (val, rec) {
                                    return val ? unescape(val) : '';
                                }
                            }, {
                                field: 'updateId',
                                title: '更新人id',
                                sortable: true,
                                hidden: true,
                                formatter: function (val, rec) {
                                    return val ? unescape(val) : '';
                                }
                            }, {
                                field: 'updateTime',
                                title: '更新时间',
                                sortable: true,
                                hidden: true,
                                formatter: function (val, rec) {
                                    return val ? unescape(val) : '';
                                }
                            }]],
                    toolbar: [
                        {
                            text: '编辑',
                            iconCls: 'icon-edit',
                            handler: function () {
                                var row = $('#dg').datagrid('getSelected');
                                if (row) {
                                    editrow(row)
                                } else {
                                    $.messager.alert('消息', '请选中一条数据');
                                }
                            }
                        }],
                    onLoadSuccess: function () {
                    },
                    onDblClickRow: function () {
                        var row = $('#dg').datagrid('getSelected');
                        editrow(row);
                    }
                });
                $('#dg').datagrid('enableFilter');
            }


            // 导出为Excel
            function exportList() {
                let startTime = $('#startTime').datebox('getValue')
                let endTime = $('#endTime').datebox('getValue')
                let status = $('#querystatus').combobox('getValue')
                var form = [];
                form.push('startTime=' + (startTime ? startTime : ''));
                form.push('endTime=' + (endTime ? endTime : ''));
                form.push('status=' + (status ? status : ''));
                let url = "oos / corrective / supervise/export"
                let xhr = new XMLHttpRequest()
                xhr.open('POST', url, true)
                xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                xhr.setRequestHeader('token', localStorage.getItem('token'));
                xhr.setRequestHeader('userType', localStorage.getItem('userType'));
                xhr.responseType = 'blob'
                xhr.onload = function () {
                    if (this.status == 200) {
                        let blob = this.response
                        let reader = new FileReader()
                        reader.readAsDataURL(blob)
                        reader.onload = function (e) {
                            let a = document.createElement('a')
                            a.download = '' + new Date().toLocaleDateString() + '-' + new Date().toLocaleTimeString() + '.xlsx'
                            a.href = e.target.result
                            $('body').append(a)
                            a.click()
                            $(a).remove()
                        }
                    }
                }
                xhr.send(form.join('&'))
            }
        </script>
        <script>
            // 主数据详情
            function detail(index) {
                const rows = $('#dg').datagrid('getRows')
                let row = rows[index]
                if (!row) {
                    $.messager.error('错误', '请至少选择一行数据')
                    return
                }
                detailAjax(row.corId)
            }

            function detailAjax(corId) {
                $.ajax({
                    type: 'get',
                    url: 'oos/corrective/supervise/selectInforDutyDept?corId=' + corId,
                    dataType: 'json',
                    success: function (data) {
                        if (data.code == 60200) {
                            let model = data.data
                            setDetail(model)
                            setTabToolBar(model)
                        } else {
                            $.messager.alert('消息', data.msg);
                        }
                    }
                })
                $('#add').window('open')
                $('#tabs').tabs('select', 0)
            }

            function setDetail(model) {
                if (!model) {
                    return
                }
                $('#corId').val(model.corId)
                $('#corNum').textbox('setValue', model.corNum)
                $('#corTitle').textbox('setValue', model.corTitle)
                const supervises = model.supervises
                initSuperviseDataGrid(supervises)

            }

            /**
             * 初始化行动项明细表
             * @param data
             */
            function initSuperviseDataGrid(data) {
                let supervise = $('#supervise')
                supervise.datagrid(
                    {
                        data: data,
                        iconCls: 'icon-application',
                        clientPaging: true,
                        remoteFilter: false,
                        filterDelay: 0,
                        width: 850,
                        height: 300,
                        nowrap: true,
                        rownumbers: true,
                        animate: false,
                        collapsible: false,
                        singleSelect: true,
                        pagination: false,
                        frozenColumns: [[]],
                        columns: [
                            [
                                {
                                    field: 'supId',
                                    title: '督办id',
                                    sortable: true,
                                    hidden: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                },
                                {
                                    field: 'corId',
                                    title: '主id',
                                    sortable: true,
                                    hidden: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'problem',
                                    title: '存在问题',
                                    sortable: true,

                                    formatter: function (val, rec) {
                                        let valTem = val ? val : '--'
                                        return "<a style=\" color: blue;text-decoration: none; margin-left: 30px;\" href=\"javascript:void(0)\" onclick=\"getSupviseDetail('" + rec.supId
                                            + "')\">" + valTem + "</a>"
                                    }
                                }, {
                                    field: 'dutyDeptCode',
                                    title: '责任部门',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(rec.dutyDeptName) : ''
                                    }
                                }, {
                                    field: 'cause',
                                    title: '原因分析',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'action',
                                    title: '预防纠正措施',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'dutyDeptName',
                                    title: '责任部门名称',
                                    sortable: true,
                                    hidden: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'dutyPersonName',
                                    title: '责任人名称',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'dutyPersonCode',
                                    title: '责任人账号',
                                    sortable: true,
                                    hidden: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'dutyPersonId',
                                    title: '责任人id',
                                    sortable: true,
                                    hidden: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'planCompleteDate',
                                    title: '计划完成日期',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'realCompleteDate',
                                    title: '实际完成日期',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'reformStatus',
                                    title: '整改效果',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'createTime',
                                    title: '创建日期',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'updateTime',
                                    title: '更新时间',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'status',
                                    title: '状态',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val == '1' ? '已完成' : '待完成';
                                    }
                                }
                            ]
                        ],
                        toolbar: [
                            {
                                text: '编辑',
                                iconCls: 'icon-edit',
                                handler: function () {
                                    let row = $('#supervise').datagrid('getSelected');
                                    if (row) {
                                        getSupviseDetail(row.supId)
                                    } else {
                                        $.messager.alert('提示', '请选择要编辑的数据')
                                        return
                                    }
                                }
                            }
                        ],
                        onEndEdit: function (index, row) {

                        },
                        onClickCell: function (rowIndex, field, value) {

                        },
                        onLoadSuccess: function () {
                        },
                        onDblClickRow: function () {
                        }
                    });
            }

            /**
             * 获取行动项详情
             * 行动项id
             * @param supId
             */
            function getSupviseDetail(supId) {
                $.ajax({
                    type: 'get',
                    url: 'oos/corrective/supervise/selectInfor?supId=' + supId,
                    dataType: 'json',
                    success: function (data) {
                        if (data.code == 60200) {
                            let supverise = data.data
                            $('#dutyPersonCode').val(supverise.dutyPersonCode)
                            $('#dutyDeptCode').val(supverise.dutyDeptCode)
                            $('#supId').val(supId)
                            // 问题
                            $('#problem').textbox('setValue', supverise.problem)
                            // 原因
                            $('#cause').textbox('setValue', supverise.cause)
                            // 预防措施
                            $('#action').textbox('setValue', supverise.action)
                            $('#planCompleteDate').textbox('setValue', supverise.planCompleteDate)
                            $('#realCompleteDate').textbox('setValue', supverise.realCompleteDate)
                            $('#dutyDeptName').textbox('setValue', supverise.dutyDeptName)
                            $('#dutyPersonName').textbox('setValue', supverise.dutyPersonName)
                            $('#reformStatus').textbox('setValue', supverise.reformStatus)
                            getFileList(supverise.supId)
                            setSuperviseToolBars(supverise.status)
                            $('#supviseDetaillWindow').window('open')
                            $('#supviseTabs').tabs('select', 0)
                        } else {
                            $.messager.alert('消息', data.msg);
                        }
                    }
                })
            }

            function setTabToolBar(row) {
                if ('0' == row.status) {
                    let supTag = false
                    if (row.supervises && row.supervises.length > 0) {
                        supTag = row.supervises.some(item => {
                            return item.status == '0'
                        })
                    }
                    if (supTag) {
                        $('#tabs').tabs(
                            {
                                tools: [{
                                    text: '保存',
                                    iconCls: 'icon-save',
                                    handler: function () {
                                        save('save');
                                    }
                                }, {
                                    text: '完成',
                                    iconCls: 'icon-ok',
                                    handler: function () {
                                        $.messager.confirm('提示', '点击完成后无法再次编辑，确定要提交吗?',
                                            function (r) {
                                                if (r) {
                                                    save('submit')
                                                }
                                            });
                                    }
                                }]
                            });
                    } else {
                        $('#tabs').tabs({
                            tools: ''
                        })
                    }

                } else {

                }
            }

            function setSuperviseToolBars(status) {
                if ('0' == status) {
                    $('#supviseTabs').tabs({
                        tools: [{
                            text: '确定',
                            iconCls: 'icon-save',
                            handler: function () {
                                if (!$('#superviesForm').form('validate')) {
                                    $.messager.alert('提示', '请完成必填项输入');
                                    return;
                                }
                                let model = {
                                    corId: $('#corId').val(),
                                    supId: $('#supId').val(),
                                    dutyPersonCode: $('#dutyPersonCode').val(),
                                    dutyDeptCode: $('#dutyDeptCode').val(),
                                    planCompleteDate: $('#planCompleteDate').datebox('getValue'),
                                    realCompleteDate: $('#realCompleteDate').textbox('getValue'),
                                    dutyDeptName: $('#dutyDeptName').textbox('getValue'),
                                    dutyPersonName: $('#dutyPersonName').textbox('getValue'),
                                    problem: $('#problem').textbox('getValue'),
                                    cause: $('#cause').textbox('getValue'),
                                    action: $('#action').textbox('getValue'),
                                    reformStatus: $('#reformStatus').textbox('getValue')
                                }
                                let superviseSelectedRow = $('#supervise').datagrid('getSelected')
                                let superviseIndex = $('#supervise').datagrid('getRowIndex', superviseSelectedRow)
                                // 更新编辑的行
                                $('#supervise').datagrid('updateRow', {
                                    index: superviseIndex,
                                    row: model
                                })
                                // 刷新行
                                $('#supervise').datagrid('refreshRow', superviseIndex)
                                // 提交时，附件不得为空
                                if ($('#checkfileList').has('li').length == 0) {
                                    $.messager.alert('提示', '请上传整改附件')
                                    return
                                }
                                saveAttch(model.supId, model, 'save')
                            }
                        }]
                    })
                } else {
                    $('#supviseTabs').tabs({
                        tools: ''
                    })
                }
            }
        </script>
        <script>
            /************文件处理部分*************/

            /**
             *文件框改变事件
             */
            function changeFile() {
                var files = $("#uploadpicker").prop("files");
                $.each(
                    files,
                    function (index, item) {
                        let choiseFile = $("#checkfileList>li");
                        if (choiseFile.length > 0) {//文件去重
                            let count = 0;
                            $.each(choiseFile, function (index1, item1) {
                                if (item.name == item1.innerText) {
                                    count++;
                                }
                            });
                            if (count == 0) {
                                if (checkFileLength(item.size)) {
                                    if (item.name.includes('#') || item.name.includes('/') || item.name.includes('&') || item.name.includes('\\') || item.name.includes(' ')) {
                                        alert('文件名不能包含&、斜杠、反斜杠、空格等特殊字符！');
                                        return;
                                    }
                                    $("#checkfileList")
                                        .append(
                                            "<li class=\"fileinfo\">"
                                            + item.name
                                            + "<a style=\"display: none; color: red; margin-left: 10px;\" href=\"javascript:void(0)\" onclick=\"RemoveFile(this,'"
                                            + item.name
                                            + "')\">删除</a>"
                                            + "</li>");
                                    selectedFileList.push(item);
                                    AddMoushover();
                                }
                            }
                        } else {
                            if (checkFileLength(item.size)) {
                                if (item.name.includes('#') || item.name.includes('/') || item.name.includes('&') || item.name.includes('\\') || item.name.includes(' ')) {
                                    alert('文件名不能包含&、斜杠、反斜杠、空格等特殊字符！');
                                    return;
                                }
                                $("#checkfileList")
                                    .append(
                                        "<li class=\"fileinfo\">"
                                        + item.name
                                        + "<a style=\"display: none; color: red; margin-left: 10px;\" href=\"javascript:void(0)\" onclick=\"RemoveFile(this,'"
                                        + item.name
                                        + "')\">删除</a>"
                                        + "</li>");
                                selectedFileList.push(item);
                                AddMoushover();
                            }
                        }
                    });
            }

            /**
             *    判断文件是否超过限制大小
             * @param fileLen
             * @returns {boolean}
             */
            function checkFileLength(fileLen) {
                if (fileLen > 102400000) {
                    $.messager.alert("提示", "上传文件大小不能超过100M", "error");
                    return false;
                }
                return true;
            }

            /**
             * //添加鼠标经过事件，鼠标划过时显示“删除”
             * @constructor
             */
            function AddMoushover() {
                $(".fileinfo").hover(function () {
                    $(this).find("a").last().css("display", "");
                }, function () {
                    $(this).find("a").last().css("display", "none");
                });
            }

            /**
             *  移除已选择的文件
             */

            function RemoveFile(obj, fileId) {
                $.messager.confirm("提示", "确认删除当前文件?", function (e) {
                    if (e) {
                        let index = $("#checkfileList>li").index($(obj).parent());
                        selectedFileList.splice(index, 1);
                        $(obj).parent().remove();

                        $.ajax({
                            type: "post",
                            url: 'file/deleteFile?fileId=' + fileId,
                            success: function (data) {
                                $.messager.alert('消息', data);
                            },
                            error: function (XMLHttpRequest, textStatus,
                                errorThrown) {
                                $.messager.alert('消息', errorThrown);
                            }
                        });

                    }
                });
            }

            /**
             * 取得该条数据所属的文件列表
             * @param row
             */
            function getFileList(id) {
                $.ajax({
                    type: "post",
                    url: "file/getFileList",
                    data: {
                        fileTable: "oos_corrective_supervise",
                        fileRow: id
                    },
                    success: function (data) {
                        const dataArray = eval(data);
                        initFile(dataArray);
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        $.messager.alert('消息', errorThrown);
                    }
                });
            }

            /**
             * 初始化该条数据的文件列表
             * @param dataArray
             */
            function initFile(dataArray) {
                $("#checkfileList").empty();
                // 			sselectedFileNameList = [];
                selectedFileList = [];
                dataArray
                    .forEach(function (e, i, a) {
                        $("#checkfileList")
                            .append(
                                "<li class=\"fileinfo\">"
                                + "<a style=\" color: blue; margin-left: 30px;\" href=\"javascript:void(0)\" onclick=\"downloadFile('"
                                + e.FILE_NAME
                                + "','"
                                + e.FILE_ID
                                + "')\">"
                                + e.FILE_NAME
                                + "</a>"
                                + "<a style=\"display: none; color: red; margin-left: 30px;\" href=\"javascript:void(0)\" onclick=\"RemoveFile(this,'"
                                + e.FILE_ID + "')\">删除</a>"
                                + "</li>");
                        // 						sselectedFileNameList.push(e.FILE_NAME);
                        AddMoushover();
                    });
            }

            // 文件下载
            function downloadFile(fileName, fileRow) {
                let url = 'api/file/download?id=' + fileRow
                let xhr = new XMLHttpRequest()
                xhr.open('GET', url, true)
                xhr.setRequestHeader('token', localStorage.getItem('tokenSupply'));
                xhr.setRequestHeader('userType', localStorage.getItem('userType'));
                xhr.responseType = 'blob'
                xhr.onload = function (data) {
                    if (this.status == 200) {
                        let blob = this.response
                        let reader = new FileReader()
                        reader.readAsDataURL(blob)
                        reader.onload = function (e) {
                            let a = document.createElement('a')
                            a.download = fileName
                            a.href = e.target.result
                            $('body').append(a)
                            a.click()
                            $(a).remove()
                        }
                    } else {

                    }
                }
                xhr.send()
            }
        </script>

        </html>